package cn.fws.meap.community.service.impl;

import java.util.List;

import cn.fws.meap.community.mapper.CreationDetailsMapper;
import cn.fws.meap.community.vo.CreationDetaileVo;
import cn.fws.meap.utils.QiNiuUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fws.framework.utils.ConvertUtils;
import cn.fws.framework.utils.UUIDUtils;

import cn.fws.meap.community.mapper.CreationMapper;
import cn.fws.meap.community.entity.Creation;
import cn.fws.meap.community.enums.ArticleTypeEnum;
import cn.fws.meap.community.service.ICreationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 创作记录 服务层实现
 *
 * @author chenkh
 * @date 2019-03-07
 */
@Service
@Transactional
public class CreationServiceImpl implements ICreationService {

    @Autowired
    private CreationMapper creationMapper;
    @Autowired
    private CreationDetailsMapper creationDetailsMapper;

    /**
     * 查询创作记录信息
     *
     * @param id 创作记录ID
     * @return 创作记录信息
     */
    @Override
    public Creation selectById(String id) {
        return creationMapper.selectById(id);
    }

    /**
     * 查询创作记录列表
     *
     * @param creation 创作记录信息
     * @return 创作记录集合
     */
    @Override
    public List<Creation> selectList(Creation creation) {
        return creationMapper.selectList(creation);
    }

    /**
     * 新增创作记录
     *
     * @param creation 创作记录信息
     * @return 结果
     */
    @Override
    public int insert(Creation creation) {
        // 设置主键
        creation.setId(UUIDUtils.getUUID());
        // 设置创建时间
        creation.setCreateTime(new java.util.Date());
        return creationMapper.insert(creation);
    }

    /**
     * 修改创作记录
     *
     * @param creation 创作记录信息
     * @return 结果
     */
    @Override
    public int update(Creation creation) {
        // 设置修改时间
        creation.setUpdateTime(new java.util.Date());
        return creationMapper.update(creation);
    }

    /**
     * 删除创作记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteByIds(String ids) {
        return creationMapper.deleteByIds(ConvertUtils.toStrArray(ids));
    }

    @Override
    public List<CreationDetaileVo> selectListUser(String openId,String sortCreateTime) {
        List<CreationDetaileVo> creationDetails = creationDetailsMapper.selectCreationDetails(openId,sortCreateTime);
        for (CreationDetaileVo creationDetaileVo : creationDetails) {
			creationDetaileVo.setArticleType(ArticleTypeEnum.statusToEnum(creationDetaileVo.getArticleType()).getName());
			String fileMsg = QiNiuUtil.getFileMsg(creationDetaileVo.getArticleMsg());
			creationDetaileVo.setArticleMsg(fileMsg);
		}
        return creationDetails;
    }

}
